package com.zl.learning.db.sqlite.sqlite;

import com.zl.learning.db.sqlite.core.JdbcTemplate;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhaolei on 2017/5/23.
 */
public class TestSqlite {
    private static JdbcTemplate jdbcTemplate = new JdbcTemplate();

    @Test
    public void test1() throws Exception {
        String sql = "select * from COMPANY";
        List<User> ms = jdbcTemplate.queryForList(sql, rs -> {
            User u = new User();
            u.setId(rs.getInt("ID"));
            return u;
        });
        ms.forEach(m -> System.out.println(m.getId()));
    }

    @Test
    public void test2() throws Exception {
        String sql = "select ADDRESS from COMPANY";
        List<String> ls = jdbcTemplate.queryForList(sql, String.class);
        ls.forEach(r -> System.out.println(r));
    }

    @Test
    public void test3() throws Exception {
        String sql = "select * from COMPANY";
        List<Map<String, Object>> ms = jdbcTemplate.queryForList(sql);
        ms.forEach(m -> {
            m.forEach((k, v) -> System.out.println(k + ":" + v));
        });
    }

    @Test
    public void test4() throws Exception {
        String sql = "select NAME from COMPANY where ID = 1";
        String name = jdbcTemplate.queryForObject(sql, String.class);
        System.out.println(name);
    }

    @Test
    public void test5() throws Exception {
        String sql = "select * from COMPANY where ID = 1";
        Map<String, String> m1 = jdbcTemplate.queryForObject(sql, rs -> {
            Map<String, String> m = new HashMap<>();
            m.put("id", rs.getString("ID"));
            m.put("name", rs.getString("NAME"));
            m.put("age", rs.getString("AGE"));
            return m;
        });
        m1.forEach((k, v) -> System.out.println(k + ":" + v));
    }

    @Test
    public void test6() throws Exception {
        String sql = "select * from COMPANY where ID = 1";
        Map<String, Object> m1 = jdbcTemplate.queryForMap(sql);
        m1.forEach((k, v) -> {
            System.out.println(k + ":" + v);
        });
    }

    @Test
    public void test7() throws Exception {
        String sql = "update COMPANY set NAME = 'zhaosi' where ID = 1";
        jdbcTemplate.update(sql);
    }
}
